home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / binutils / nlmconv.c < prev    next >
C/C++ Source or Header  |  1994-10-10  |  68KB  |  2,347 lines

  1. /* nlmconv.c -- NLM conversion program
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Binutils.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Written by Ian Lance Taylor <ian@cygnus.com>.
  21.  
  22.    This program can be used to convert any appropriate object file
  23.    into a NetWare Loadable Module (an NLM).  It will accept a linker
  24.    specification file which is identical to that accepted by the
  25.    NetWare linker, NLMLINK, except that the INPUT command, normally
  26.    used to give a list of object files to link together, is not used.
  27.    This program will convert only a single object file.  */
  28.  
  29. #include <ansidecl.h>
  30. #include <stdio.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <sys/file.h>
  36. #include <assert.h>
  37. #include <getopt.h>
  38. #include "bfd.h"
  39. #include "libiberty.h"
  40. #include "sysdep.h"
  41. #include "bucomm.h"
  42. /* Internal BFD NLM header.  */
  43. #include "libnlm.h"
  44. #include "nlmconv.h"
  45.  
  46. #ifdef NLMCONV_ALPHA
  47. #include "coff/sym.h"
  48. #include "coff/ecoff.h"
  49. #endif
  50.  
  51. /* If strerror is just a macro, we want to use the one from libiberty
  52.    since it will handle undefined values.  */
  53. #undef strerror
  54. extern char *strerror ();
  55.  
  56. #ifndef localtime
  57. extern struct tm *localtime ();
  58. #endif
  59.  
  60. #ifndef getenv
  61. extern char *getenv ();
  62. #endif
  63.  
  64. #ifndef SEEK_SET
  65. #define SEEK_SET 0
  66. #endif
  67.  
  68. #ifndef R_OK
  69. #define R_OK 4
  70. #define W_OK 2
  71. #define X_OK 1
  72. #endif
  73.  
  74. /* Global variables.  */
  75.  
  76. /* The name used to invoke the program.  */
  77. char *program_name;
  78.  
  79. /* The version number.  */
  80. extern char *program_version;
  81.  
  82. /* Local variables.  */
  83.  
  84. /* Whether to print out debugging information (currently just controls
  85.    whether it prints the linker command if there is one).  */
  86. static int debug;
  87.  
  88. /* The symbol table.  */
  89. static asymbol **symbols;
  90.  
  91. /* A section we create in the output file to hold pointers to where
  92.    the sections of the input file end up.  We will put a pointer to
  93.    this section in the NLM header.  These is an entry for each input
  94.    section.  The format is
  95.        null terminated section name
  96.        zeroes to adjust to 4 byte boundary
  97.        4 byte section data file pointer
  98.        4 byte section size
  99.    We don't need a version number.  The way we find this information
  100.    is by finding a stamp in the NLM header information.  If we need to
  101.    change the format of this information, we can simply change the
  102.    stamp.  */
  103. static asection *secsec;
  104.  
  105. /* A temporary file name to be unlinked on exit.  Actually, for most
  106.    errors, we leave it around.  It's not clear whether that is helpful
  107.    or not.  */
  108. static char *unlink_on_exit;
  109.  
  110. /* The list of long options.  */
  111. static struct option long_options[] =
  112. {
  113.   { "debug", no_argument, 0, 'd' },
  114.   { "header-file", required_argument, 0, 'T' },
  115.   { "help", no_argument, 0, 'h' },
  116.   { "input-target", required_argument, 0, 'I' },
  117.   { "input-format", required_argument, 0, 'I' }, /* Obsolete */
  118.   { "linker", required_argument, 0, 'l' },
  119.   { "output-target", required_argument, 0, 'O' },
  120.   { "output-format", required_argument, 0, 'O' }, /* Obsolete */
  121.   { "version", no_argument, 0, 'V' },
  122.   { NULL, no_argument, 0, 0 }
  123. };
  124.  
  125. /* Local routines.  */
  126.  
  127. static void show_help PARAMS ((void));
  128. static void show_usage PARAMS ((FILE *, int));
  129. static const char *select_output_format PARAMS ((enum bfd_architecture,
  130.                          unsigned long, boolean));
  131. static void setup_sections PARAMS ((bfd *, asection *, PTR));
  132. static void copy_sections PARAMS ((bfd *, asection *, PTR));
  133. static void mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
  134.                    long *, char *,
  135.                    bfd_size_type));
  136. static void default_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
  137.                        long *, char *,
  138.                        bfd_size_type));
  139. static char *link_inputs PARAMS ((struct string_list *, char *));
  140. static const char *choose_temp_base_try PARAMS ((const char *,
  141.                          const char *));
  142. static void choose_temp_base PARAMS ((void));
  143. static int pexecute PARAMS ((char *, char *[]));
  144.  
  145. #ifdef NLMCONV_I386
  146. static void i386_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
  147.                     long *, char *,
  148.                     bfd_size_type));
  149. #endif
  150.  
  151. #ifdef NLMCONV_ALPHA
  152. static void alpha_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
  153.                      long *, char *,
  154.                      bfd_size_type));
  155. #endif
  156.  
  157. #ifdef NLMCONV_POWERPC
  158. static void powerpc_build_stubs PARAMS ((bfd *, bfd *, asymbol ***, long *));
  159. static void powerpc_resolve_stubs PARAMS ((bfd *, bfd *));
  160. static void powerpc_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
  161.                        long *, char *,
  162.                        bfd_size_type));
  163. #endif
  164.  
  165. /* The main routine.  */
  166.  
  167. int
  168. main (argc, argv)
  169.      int argc;
  170.      char **argv;
  171. {
  172.   int opt;
  173.   char *input_file = NULL;
  174.   const char *input_format = NULL;
  175.   const char *output_format = NULL;
  176.   const char *header_file = NULL;
  177.   char *ld_arg = NULL;
  178.   Nlm_Internal_Fixed_Header fixed_hdr_struct;
  179.   Nlm_Internal_Variable_Header var_hdr_struct;
  180.   Nlm_Internal_Version_Header version_hdr_struct;
  181.   Nlm_Internal_Copyright_Header copyright_hdr_struct;
  182.   Nlm_Internal_Extended_Header extended_hdr_struct;
  183.   bfd *inbfd;
  184.   bfd *outbfd;
  185.   asymbol **newsyms, **outsyms;
  186.   long symcount, newsymalloc, newsymcount;
  187.   long symsize;
  188.   asection *text_sec, *bss_sec, *data_sec;
  189.   bfd_vma vma;
  190.   bfd_size_type align;
  191.   asymbol *endsym;
  192.   long i;
  193.   char inlead, outlead;
  194.   boolean gotstart, gotexit, gotcheck;
  195.   struct stat st;
  196.   FILE *custom_data, *help_data, *message_data, *rpc_data, *shared_data;
  197.   size_t custom_size, help_size, message_size, module_size, rpc_size;
  198.   asection *custom_section, *help_section, *message_section, *module_section;
  199.   asection *rpc_section, *shared_section;
  200.   bfd *sharedbfd;
  201.   size_t shared_offset, shared_size;
  202.   Nlm_Internal_Fixed_Header sharedhdr;
  203.   int len;
  204.   char *modname;
  205.   char **matching;
  206.  
  207.   program_name = argv[0];
  208.   xmalloc_set_program_name (program_name);
  209.  
  210.   bfd_init ();
  211.  
  212.   while ((opt = getopt_long (argc, argv, "dhI:l:O:T:V", long_options,
  213.                  (int *) NULL))
  214.      != EOF)
  215.     {
  216.       switch (opt)
  217.     {
  218.     case 'd':
  219.       debug = 1;
  220.       break;
  221.     case 'h':
  222.       show_help ();
  223.       /*NOTREACHED*/
  224.     case 'I':
  225.       input_format = optarg;
  226.       break;
  227.     case 'l':
  228.       ld_arg = optarg;
  229.       break;
  230.     case 'O':
  231.       output_format = optarg;
  232.       break;
  233.     case 'T':
  234.       header_file = optarg;
  235.       break;
  236.     case 'V':
  237.       printf ("GNU %s version %s\n", program_name, program_version);
  238.       exit (0);
  239.       /*NOTREACHED*/
  240.     case 0:
  241.       break;
  242.     default:
  243.       show_usage (stderr, 1);
  244.       /*NOTREACHED*/
  245.     }
  246.     }
  247.  
  248.   /* The input and output files may be named on the command line.  */
  249.   output_file = NULL;
  250.   if (optind < argc)
  251.     {
  252.       input_file = argv[optind];
  253.       ++optind;
  254.       if (optind < argc)
  255.     {
  256.       output_file = argv[optind];
  257.       ++optind;
  258.       if (optind < argc)
  259.         show_usage (stderr, 1);
  260.       if (strcmp (input_file, output_file) == 0)
  261.         {
  262.           fprintf (stderr,
  263.                "%s: input and output files must be different\n",
  264.                program_name);
  265.           exit (1);
  266.         }
  267.     }
  268.     }
  269.  
  270.   /* Initialize the header information to default values.  */
  271.   fixed_hdr = &fixed_hdr_struct;
  272.   memset ((PTR) &fixed_hdr_struct, 0, sizeof fixed_hdr_struct);
  273.   var_hdr = &var_hdr_struct;
  274.   memset ((PTR) &var_hdr_struct, 0, sizeof var_hdr_struct);
  275.   version_hdr = &version_hdr_struct;
  276.   memset ((PTR) &version_hdr_struct, 0, sizeof version_hdr_struct);
  277.   copyright_hdr = ©right_hdr_struct;
  278.   memset ((PTR) ©right_hdr_struct, 0, sizeof copyright_hdr_struct);
  279.   extended_hdr = &extended_hdr_struct;
  280.   memset ((PTR) &extended_hdr_struct, 0, sizeof extended_hdr_struct);
  281.   check_procedure = NULL;
  282.   custom_file = NULL;
  283.   debug_info = false;
  284.   exit_procedure = "_Stop";
  285.   export_symbols = NULL;
  286.   map_file = NULL;
  287.   full_map = false;
  288.   help_file = NULL;
  289.   import_symbols = NULL;
  290.   message_file = NULL;
  291.   modules = NULL;
  292.   sharelib_file = NULL;
  293.   start_procedure = "_Prelude";
  294.   verbose = false;
  295.   rpc_file = NULL;
  296.  
  297.   parse_errors = 0;
  298.  
  299.   /* Parse the header file (if there is one).  */
  300.   if (header_file != NULL)
  301.     {
  302.       if (! nlmlex_file (header_file)
  303.       || yyparse () != 0
  304.       || parse_errors != 0)
  305.     exit (1);
  306.     }
  307.  
  308.   if (input_files != NULL)
  309.     {
  310.       if (input_file != NULL)
  311.     {
  312.       fprintf (stderr,
  313.            "%s: input file named both on command line and with INPUT\n",
  314.            program_name);
  315.       exit (1);
  316.     }
  317.       if (input_files->next == NULL)
  318.     input_file = input_files->string;
  319.       else
  320.     input_file = link_inputs (input_files, ld_arg);
  321.     }
  322.   else if (input_file == NULL)
  323.     {
  324.       fprintf (stderr, "%s: no input file\n", program_name);
  325.       show_usage (stderr, 1);
  326.     }
  327.  
  328.   inbfd = bfd_openr (input_file, input_format);
  329.   if (inbfd == NULL)
  330.     bfd_fatal (input_file);
  331.  
  332.   if (! bfd_check_format_matches (inbfd, bfd_object, &matching))
  333.     {
  334.       bfd_nonfatal (input_file);
  335.       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  336.     {
  337.       list_matching_formats (matching);
  338.       free (matching);
  339.     }
  340.       exit (1);
  341.     }
  342.  
  343.   if (output_format == NULL)
  344.     output_format = select_output_format (bfd_get_arch (inbfd),
  345.                       bfd_get_mach (inbfd),
  346.                       inbfd->xvec->byteorder_big_p);
  347.  
  348.   assert (output_format != NULL);
  349.  
  350.   /* Use the output file named on the command line if it exists.
  351.      Otherwise use the file named in the OUTPUT statement.  */
  352.   if (output_file == NULL)
  353.     {
  354.       fprintf (stderr, "%s: no name for output file\n",
  355.            program_name);
  356.       show_usage (stderr, 1);
  357.     }
  358.  
  359.   outbfd = bfd_openw (output_file, output_format);
  360.   if (outbfd == NULL)
  361.     bfd_fatal (output_file);
  362.   if (! bfd_set_format (outbfd, bfd_object))
  363.     bfd_fatal (output_file);
  364.  
  365.   assert (bfd_get_flavour (outbfd) == bfd_target_nlm_flavour);
  366.  
  367.   if (bfd_arch_get_compatible (inbfd, outbfd) == NULL)
  368.     fprintf (stderr,
  369.          "%s: warning:input and output formats are not compatible\n",
  370.          program_name);
  371.  
  372.   /* Move the values read from the command file into outbfd.  */
  373.   *nlm_fixed_header (outbfd) = fixed_hdr_struct;
  374.   *nlm_variable_header (outbfd) = var_hdr_struct;
  375.   *nlm_version_header (outbfd) = version_hdr_struct;
  376.   *nlm_copyright_header (outbfd) = copyright_hdr_struct;
  377.   *nlm_extended_header (outbfd) = extended_hdr_struct;
  378.  
  379.   /* Start copying the input BFD to the output BFD.  */
  380.   if (! bfd_set_file_flags (outbfd, bfd_get_file_flags (inbfd)))
  381.     bfd_fatal (bfd_get_filename (outbfd));
  382.  
  383.   symsize = bfd_get_symtab_upper_bound (inbfd);
  384.   if (symsize < 0)
  385.     bfd_fatal (input_file);
  386.   symbols = (asymbol **) xmalloc (symsize);
  387.   symcount = bfd_canonicalize_symtab (inbfd, symbols);
  388.   if (symcount < 0)
  389.     bfd_fatal (input_file);
  390.  
  391.   /* Make sure we have a .bss section.  */
  392.   bss_sec = bfd_get_section_by_name (outbfd, NLM_UNINITIALIZED_DATA_NAME);
  393.   if (bss_sec == NULL)
  394.     {
  395.       bss_sec = bfd_make_section (outbfd, NLM_UNINITIALIZED_DATA_NAME);
  396.       if (bss_sec == NULL
  397.       || ! bfd_set_section_flags (outbfd, bss_sec, SEC_ALLOC)
  398.       || ! bfd_set_section_alignment (outbfd, bss_sec, 1))
  399.     bfd_fatal ("make .bss section");
  400.     }
  401.  
  402.   /* We store the original section names in the .nlmsections section,
  403.      so that programs which understand it can resurrect the original
  404.      sections from the NLM.  We will put a pointer to .nlmsections in
  405.      the NLM header area.  */
  406.   secsec = bfd_make_section (outbfd, ".nlmsections");
  407.   if (secsec == NULL)
  408.     bfd_fatal ("make .nlmsections section");
  409.   if (! bfd_set_section_flags (outbfd, secsec, SEC_HAS_CONTENTS))
  410.     bfd_fatal ("set .nlmsections flags");
  411.  
  412. #ifdef NLMCONV_POWERPC
  413.   /* For PowerPC NetWare we need to build stubs for calls to undefined
  414.      symbols.  Because each stub requires an entry in the TOC section
  415.      which must be at the same location as other entries in the TOC
  416.      section, we must do this before determining where the TOC section
  417.      goes in setup_sections.  */
  418.   if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
  419.     powerpc_build_stubs (inbfd, outbfd, &symbols, &symcount);
  420. #endif
  421.  
  422.   /* Set up the sections.  */
  423.   bfd_map_over_sections (inbfd, setup_sections, (PTR) outbfd);
  424.  
  425.   text_sec = bfd_get_section_by_name (outbfd, NLM_CODE_NAME);
  426.  
  427.   /* The .bss section immediately follows the .data section.  */
  428.   data_sec = bfd_get_section_by_name (outbfd, NLM_INITIALIZED_DATA_NAME);
  429.   if (data_sec != NULL)
  430.     {
  431.       bfd_size_type add;
  432.  
  433.       vma = bfd_get_section_size_before_reloc (data_sec);
  434.       align = 1 << bss_sec->alignment_power;
  435.       add = ((vma + align - 1) &~ (align - 1)) - vma;
  436.       vma += add;
  437.       if (! bfd_set_section_vma (outbfd, bss_sec, vma))
  438.     bfd_fatal ("set .bss vma");
  439.       if (add != 0)
  440.     {
  441.       bfd_size_type data_size;
  442.  
  443.       data_size = bfd_get_section_size_before_reloc (data_sec);
  444.       if (! bfd_set_section_size (outbfd, data_sec, data_size + add))
  445.         bfd_fatal ("set .data size");
  446.     }
  447.     }
  448.  
  449.   /* Adjust symbol information.  */
  450.   inlead = bfd_get_symbol_leading_char (inbfd);
  451.   outlead = bfd_get_symbol_leading_char (outbfd);
  452.   gotstart = false;
  453.   gotexit = false;
  454.   gotcheck = false;
  455.   newsymalloc = 10;
  456.   newsyms = (asymbol **) xmalloc (newsymalloc * sizeof (asymbol *));
  457.   newsymcount = 0;
  458.   endsym = NULL;
  459.   for (i = 0; i < symcount; i++)
  460.     {
  461.       register asymbol *sym;
  462.  
  463.       sym = symbols[i];
  464.  
  465.       /* Add or remove a leading underscore.  */
  466.       if (inlead != outlead)
  467.     {
  468.       if (inlead != '\0')
  469.         {
  470.           if (bfd_asymbol_name (sym)[0] == inlead)
  471.         {
  472.           if (outlead == '\0')
  473.             ++sym->name;
  474.           else
  475.             {
  476.               char *new;
  477.  
  478.               new = xmalloc (strlen (bfd_asymbol_name (sym)) + 1);
  479.               new[0] = outlead;
  480.               strcpy (new + 1, bfd_asymbol_name (sym) + 1);
  481.               sym->name = new;
  482.             }
  483.         }
  484.         }
  485.       else
  486.         {
  487.           char *new;
  488.  
  489.           new = xmalloc (strlen (bfd_asymbol_name (sym)) + 2);
  490.           new[0] = outlead;
  491.           strcpy (new + 1, bfd_asymbol_name (sym));
  492.           sym->name = new;
  493.         }
  494.     }
  495.  
  496.       /* NLM's have an uninitialized data section, but they do not
  497.      have a common section in the Unix sense.  Move all common
  498.      symbols into the .bss section, and mark them as exported.  */
  499.       if (bfd_is_com_section (bfd_get_section (sym)))
  500.     {
  501.       bfd_vma size;
  502.  
  503.       sym->section = bss_sec;
  504.       size = sym->value;
  505.       sym->value = bss_sec->_raw_size;
  506.       bss_sec->_raw_size += size;
  507.       align = 1 << bss_sec->alignment_power;
  508.       bss_sec->_raw_size = (bss_sec->_raw_size + align - 1) &~ (align - 1);
  509.       sym->flags |= BSF_EXPORT | BSF_GLOBAL;
  510.     }
  511.       else if (bfd_get_section (sym)->output_section != NULL)
  512.     {
  513.       /* Move the symbol into the output section.  */
  514.       sym->value += bfd_get_section (sym)->output_offset;
  515.       sym->section = bfd_get_section (sym)->output_section;
  516.       /* This is no longer a section symbol.  */
  517.       sym->flags &=~ BSF_SECTION_SYM;
  518.     }
  519.  
  520.       /* Force _edata and _end to be defined.  This would normally be
  521.      done by the linker, but the manipulation of the common
  522.      symbols will confuse it.  */
  523.       if ((sym->flags & BSF_DEBUGGING) == 0
  524.       && bfd_asymbol_name (sym)[0] == '_'
  525.       && bfd_is_und_section (bfd_get_section (sym)))
  526.     {
  527.       if (strcmp (bfd_asymbol_name (sym), "_edata") == 0)
  528.         {
  529.           sym->section = bss_sec;
  530.           sym->value = 0;
  531.         }
  532.       if (strcmp (bfd_asymbol_name (sym), "_end") == 0)
  533.         {
  534.           sym->section = bss_sec;
  535.           endsym = sym;
  536.         }
  537.  
  538. #ifdef NLMCONV_POWERPC
  539.       /* For PowerPC NetWare, we define __GOT0.  This is the start
  540.          of the .got section.  */
  541.       if (bfd_get_arch (inbfd) == bfd_arch_powerpc
  542.           && strcmp (bfd_asymbol_name (sym), "__GOT0") == 0)
  543.         {
  544.           asection *got_sec;
  545.  
  546.           got_sec = bfd_get_section_by_name (inbfd, ".got");
  547.           assert (got_sec != (asection *) NULL);
  548.           sym->value = got_sec->output_offset;
  549.           sym->section = got_sec->output_section;
  550.         }
  551. #endif
  552.      }
  553.  
  554.       /* If this is a global symbol, check the export list.  */
  555.       if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) != 0)
  556.     {
  557.       register struct string_list *l;
  558.       int found_simple;
  559.  
  560.       /* Unfortunately, a symbol can appear multiple times on the
  561.          export list, with and without prefixes.  */
  562.       found_simple = 0;
  563.       for (l = export_symbols; l != NULL; l = l->next)
  564.         {
  565.           if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
  566.         found_simple = 1;
  567.           else
  568.         {
  569.           char *zbase;
  570.  
  571.           zbase = strchr (l->string, '@');
  572.           if (zbase != NULL
  573.               && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
  574.             {
  575.               /* We must add a symbol with this prefix.  */
  576.               if (newsymcount >= newsymalloc)
  577.             {
  578.               newsymalloc += 10;
  579.               newsyms = ((asymbol **)
  580.                      xrealloc ((PTR) newsyms,
  581.                            (newsymalloc
  582.                         * sizeof (asymbol *))));
  583.             }
  584.               newsyms[newsymcount] =
  585.             (asymbol *) xmalloc (sizeof (asymbol));
  586.               *newsyms[newsymcount] = *sym;
  587.               newsyms[newsymcount]->name = l->string;
  588.               ++newsymcount;
  589.             }
  590.         }
  591.         }
  592.       if (! found_simple)
  593.         {
  594.           /* The unmodified symbol is actually not exported at
  595.          all.  */
  596.           sym->flags &=~ (BSF_GLOBAL | BSF_EXPORT);
  597.           sym->flags |= BSF_LOCAL;
  598.         }
  599.     }
  600.  
  601.       /* If it's an undefined symbol, see if it's on the import list.
  602.      Change the prefix if necessary.  */
  603.       if (bfd_is_und_section (bfd_get_section (sym)))
  604.     {
  605.       register struct string_list *l;
  606.  
  607.       for (l = import_symbols; l != NULL; l = l->next)
  608.         {
  609.           if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
  610.         break;
  611.           else
  612.         {
  613.           char *zbase;
  614.  
  615.           zbase = strchr (l->string, '@');
  616.           if (zbase != NULL
  617.               && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
  618.             {
  619.               sym->name = l->string;
  620.               break;
  621.             }
  622.         }
  623.         }
  624.       if (l == NULL)
  625.         fprintf (stderr,
  626.              "%s: warning: symbol %s imported but not in import list\n",
  627.              program_name, bfd_asymbol_name (sym));
  628.     }
  629.     
  630.       /* See if it's one of the special named symbols.  */
  631.       if ((sym->flags & BSF_DEBUGGING) == 0)
  632.     {
  633.       bfd_vma val;
  634.  
  635.       /* FIXME: If these symbols are not in the .text section, we
  636.          add the .text section size to the value.  This may not be
  637.          correct for all targets.  I'm not sure how this should
  638.          really be handled.  */
  639.       if (strcmp (bfd_asymbol_name (sym), start_procedure) == 0)
  640.         {
  641.           val = bfd_asymbol_value (sym);
  642.           if (bfd_get_section (sym) == data_sec
  643.           && text_sec != (asection *) NULL)
  644.         val += bfd_section_size (outbfd, text_sec);
  645.           if (! bfd_set_start_address (outbfd, val))
  646.         bfd_fatal ("set start address");
  647.           gotstart = true;
  648.         }
  649.       if (strcmp (bfd_asymbol_name (sym), exit_procedure) == 0)
  650.         {
  651.           val = bfd_asymbol_value (sym);
  652.           if (bfd_get_section (sym) == data_sec
  653.           && text_sec != (asection *) NULL)
  654.         val += bfd_section_size (outbfd, text_sec);
  655.           nlm_fixed_header (outbfd)->exitProcedureOffset = val;
  656.           gotexit = true;
  657.         }
  658.       if (check_procedure != NULL
  659.           && strcmp (bfd_asymbol_name (sym), check_procedure) == 0)
  660.         {
  661.           val = bfd_asymbol_value (sym);
  662.           if (bfd_get_section (sym) == data_sec
  663.           && text_sec != (asection *) NULL)
  664.         val += bfd_section_size (outbfd, text_sec);
  665.           nlm_fixed_header (outbfd)->checkUnloadProcedureOffset = val;
  666.           gotcheck = true;
  667.         }
  668.     }
  669.     }
  670.  
  671.   if (endsym != NULL)
  672.     {
  673.       endsym->value = bfd_get_section_size_before_reloc (bss_sec);
  674.  
  675.       /* FIXME: If any relocs referring to _end use inplace addends,
  676.      then I think they need to be updated.  This is handled by
  677.      i386_mangle_relocs.  Is it needed for any other object
  678.      formats?  */
  679.     }
  680.  
  681.   if (newsymcount == 0)
  682.     outsyms = symbols;
  683.   else
  684.     {
  685.       outsyms = (asymbol **) xmalloc ((symcount + newsymcount + 1)
  686.                       * sizeof (asymbol *));
  687.       memcpy (outsyms, symbols, symcount * sizeof (asymbol *));
  688.       memcpy (outsyms + symcount, newsyms, newsymcount * sizeof (asymbol *));
  689.       outsyms[symcount + newsymcount] = NULL;
  690.     }
  691.  
  692.   bfd_set_symtab (outbfd, outsyms, symcount + newsymcount);
  693.     
  694.   if (! gotstart)
  695.     fprintf (stderr, "%s: warning: START procedure %s not defined\n",
  696.          program_name, start_procedure);
  697.   if (! gotexit)
  698.     fprintf (stderr, "%s: warning: EXIT procedure %s not defined\n",
  699.          program_name, exit_procedure);
  700.   if (check_procedure != NULL
  701.       && ! gotcheck)
  702.     fprintf (stderr, "%s: warning: CHECK procedure %s not defined\n",
  703.          program_name, check_procedure);
  704.  
  705.   /* Add additional sections required for the header information.  */
  706.   if (custom_file != NULL)
  707.     {
  708.       custom_data = fopen (custom_file, "r");
  709.       if (custom_data == NULL
  710.       || fstat (fileno (custom_data), &st) < 0)
  711.     {
  712.       fprintf (stderr, "%s:%s: %s\n", program_name, custom_file,
  713.            strerror (errno));
  714.       custom_file = NULL;
  715.     }
  716.       else
  717.     {
  718.       custom_size = st.st_size;
  719.       custom_section = bfd_make_section (outbfd, ".nlmcustom");
  720.       if (custom_section == NULL
  721.           || ! bfd_set_section_size (outbfd, custom_section, custom_size)
  722.           || ! bfd_set_section_flags (outbfd, custom_section,
  723.                       SEC_HAS_CONTENTS))
  724.         bfd_fatal ("custom section");
  725.     }
  726.     }
  727.   if (help_file != NULL)
  728.     {
  729.       help_data = fopen (help_file, "r");
  730.       if (help_data == NULL
  731.       || fstat (fileno (help_data), &st) < 0)
  732.     {
  733.       fprintf (stderr, "%s:%s: %s\n", program_name, help_file,
  734.            strerror (errno));
  735.       help_file = NULL;
  736.     }
  737.       else
  738.     {
  739.       help_size = st.st_size;
  740.       help_section = bfd_make_section (outbfd, ".nlmhelp");
  741.       if (help_section == NULL
  742.           || ! bfd_set_section_size (outbfd, help_section, help_size)
  743.           || ! bfd_set_section_flags (outbfd, help_section,
  744.                       SEC_HAS_CONTENTS))
  745.         bfd_fatal ("help section");
  746.       strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
  747.     }
  748.     }
  749.   if (message_file != NULL)
  750.     {
  751.       message_data = fopen (message_file, "r");
  752.       if (message_data == NULL
  753.       || fstat (fileno (message_data), &st) < 0)
  754.     {
  755.       fprintf (stderr, "%s:%s: %s\n", program_name, message_file,
  756.            strerror (errno));
  757.       message_file = NULL;
  758.     }
  759.       else
  760.     {
  761.       message_size = st.st_size;
  762.       message_section = bfd_make_section (outbfd, ".nlmmessages");
  763.       if (message_section == NULL
  764.           || ! bfd_set_section_size (outbfd, message_section, message_size)
  765.           || ! bfd_set_section_flags (outbfd, message_section,
  766.                       SEC_HAS_CONTENTS))
  767.         bfd_fatal ("message section");
  768.       strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
  769.     }
  770.     }
  771.   if (modules != NULL)
  772.     {
  773.       struct string_list *l;
  774.  
  775.       module_size = 0;
  776.       for (l = modules; l != NULL; l = l->next)
  777.     module_size += strlen (l->string) + 1;
  778.       module_section = bfd_make_section (outbfd, ".nlmmodules");
  779.       if (module_section == NULL
  780.       || ! bfd_set_section_size (outbfd, module_section, module_size)
  781.       || ! bfd_set_section_flags (outbfd, module_section,
  782.                       SEC_HAS_CONTENTS))
  783.     bfd_fatal ("module section");
  784.     }
  785.   if (rpc_file != NULL)
  786.     {
  787.       rpc_data = fopen (rpc_file, "r");
  788.       if (rpc_data == NULL
  789.       || fstat (fileno (rpc_data), &st) < 0)
  790.     {
  791.       fprintf (stderr, "%s:%s: %s\n", program_name, rpc_file,
  792.            strerror (errno));
  793.       rpc_file = NULL;
  794.     }
  795.       else
  796.     {
  797.       rpc_size = st.st_size;
  798.       rpc_section = bfd_make_section (outbfd, ".nlmrpc");
  799.       if (rpc_section == NULL
  800.           || ! bfd_set_section_size (outbfd, rpc_section, rpc_size)
  801.           || ! bfd_set_section_flags (outbfd, rpc_section,
  802.                       SEC_HAS_CONTENTS))
  803.         bfd_fatal ("rpc section");
  804.       strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
  805.     }
  806.     }
  807.   if (sharelib_file != NULL)
  808.     {
  809.       sharedbfd = bfd_openr (sharelib_file, output_format);
  810.       if (sharedbfd == NULL
  811.       || ! bfd_check_format (sharedbfd, bfd_object))
  812.     {
  813.       fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
  814.            bfd_errmsg (bfd_get_error ()));
  815.       sharelib_file = NULL;
  816.     }
  817.       else
  818.     {
  819.       sharedhdr = *nlm_fixed_header (sharedbfd);
  820.       bfd_close (sharedbfd);
  821.       shared_data = fopen (sharelib_file, "r");
  822.       if (shared_data == NULL
  823.           || (fstat (fileno (shared_data), &st) < 0))
  824.         {
  825.           fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
  826.                strerror (errno));
  827.           sharelib_file = NULL;
  828.         }
  829.       else
  830.         {
  831.           /* If we were clever, we could just copy out the
  832.          sections of the shared library which we actually
  833.          need.  However, we would have to figure out the sizes
  834.          of the external and public information, and that can
  835.          not be done without reading through them.  */
  836.           if (sharedhdr.uninitializedDataSize > 0)
  837.         {
  838.           /* There is no place to record this information.  */
  839.           fprintf (stderr,
  840.                "%s:%s: warning: shared libraries can not have uninitialized data\n",
  841.                program_name, sharelib_file);
  842.         }
  843.           shared_offset = st.st_size;
  844.           if (shared_offset > sharedhdr.codeImageOffset)
  845.         shared_offset = sharedhdr.codeImageOffset;
  846.           if (shared_offset > sharedhdr.dataImageOffset)
  847.         shared_offset = sharedhdr.dataImageOffset;
  848.           if (shared_offset > sharedhdr.relocationFixupOffset)
  849.         shared_offset = sharedhdr.relocationFixupOffset;
  850.           if (shared_offset > sharedhdr.externalReferencesOffset)
  851.         shared_offset = sharedhdr.externalReferencesOffset;
  852.           if (shared_offset > sharedhdr.publicsOffset)
  853.         shared_offset = sharedhdr.publicsOffset;
  854.           shared_size = st.st_size - shared_offset;
  855.           shared_section = bfd_make_section (outbfd, ".nlmshared");
  856.           if (shared_section == NULL
  857.           || ! bfd_set_section_size (outbfd, shared_section,
  858.                          shared_size)
  859.           || ! bfd_set_section_flags (outbfd, shared_section,
  860.                           SEC_HAS_CONTENTS))
  861.         bfd_fatal ("shared section");
  862.           strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
  863.         }
  864.     }
  865.     }
  866.  
  867.   /* Check whether a version was given.  */
  868.   if (strncmp (version_hdr->stamp, "VeRsIoN#", 8) != 0)
  869.     fprintf (stderr, "%s: warning: No version number given\n",
  870.          program_name);
  871.  
  872.   /* At least for now, always create an extended header, because that
  873.      is what NLMLINK does.  */
  874.   strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
  875.  
  876.   strncpy (nlm_cygnus_ext_header (outbfd)->stamp, "CyGnUsEx", 8);
  877.  
  878.   /* If the date was not given, force it in.  */
  879.   if (nlm_version_header (outbfd)->month == 0
  880.       && nlm_version_header (outbfd)->day == 0
  881.       && nlm_version_header (outbfd)->year == 0)
  882.     {
  883.       time_t now;
  884.       struct tm *ptm;
  885.  
  886.       time (&now);
  887.       ptm = localtime (&now);
  888.       nlm_version_header (outbfd)->month = ptm->tm_mon + 1;
  889.       nlm_version_header (outbfd)->day = ptm->tm_mday;
  890.       nlm_version_header (outbfd)->year = ptm->tm_year + 1900;
  891.       strncpy (version_hdr->stamp, "VeRsIoN#", 8);
  892.     }
  893.  
  894. #ifdef NLMCONV_POWERPC
  895.   /* Resolve the stubs we build for PowerPC NetWare.  */
  896.   if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
  897.     powerpc_resolve_stubs (inbfd, outbfd);
  898. #endif
  899.  
  900.   /* Copy over the sections.  */
  901.   bfd_map_over_sections (inbfd, copy_sections, (PTR) outbfd);
  902.  
  903.   /* Finish up the header information.  */
  904.   if (custom_file != NULL)
  905.     {
  906.       PTR data;
  907.  
  908.       data = xmalloc (custom_size);
  909.       if (fread (data, 1, custom_size, custom_data) != custom_size)
  910.     fprintf (stderr, "%s:%s: read: %s\n", program_name, custom_file,
  911.          strerror (errno));
  912.       else
  913.     {
  914.       if (! bfd_set_section_contents (outbfd, custom_section, data,
  915.                       (file_ptr) 0, custom_size))
  916.         bfd_fatal ("custom section");
  917.       nlm_fixed_header (outbfd)->customDataOffset =
  918.         custom_section->filepos;
  919.       nlm_fixed_header (outbfd)->customDataSize = custom_size;
  920.     }
  921.       free (data);
  922.     }
  923.   if (! debug_info)
  924.     {
  925.       /* As a special hack, the backend recognizes a debugInfoOffset
  926.      of -1 to mean that it should not output any debugging
  927.      information.  This can not be handling by fiddling with the
  928.      symbol table because exported symbols appear in both the
  929.      export information and the debugging information.  */
  930.       nlm_fixed_header (outbfd)->debugInfoOffset = (file_ptr) -1;
  931.     }
  932.   if (map_file != NULL)
  933.     fprintf (stderr,
  934.          "%s: warning: MAP and FULLMAP are not supported; try ld -M\n",
  935.          program_name);
  936.   if (help_file != NULL)
  937.     {
  938.       PTR data;
  939.  
  940.       data = xmalloc (help_size);
  941.       if (fread (data, 1, help_size, help_data) != help_size)
  942.     fprintf (stderr, "%s:%s: read: %s\n", program_name, help_file,
  943.          strerror (errno));
  944.       else
  945.     {
  946.       if (! bfd_set_section_contents (outbfd, help_section, data,
  947.                       (file_ptr) 0, help_size))
  948.         bfd_fatal ("help section");
  949.       nlm_extended_header (outbfd)->helpFileOffset =
  950.         help_section->filepos;
  951.       nlm_extended_header (outbfd)->helpFileLength = help_size;
  952.     }
  953.       free (data);
  954.     }
  955.   if (message_file != NULL)
  956.     {
  957.       PTR data;
  958.  
  959.       data = xmalloc (message_size);
  960.       if (fread (data, 1, message_size, message_data) != message_size)
  961.     fprintf (stderr, "%s:%s: read: %s\n", program_name, message_file,
  962.          strerror (errno));
  963.       else
  964.     {
  965.       if (! bfd_set_section_contents (outbfd, message_section, data,
  966.                       (file_ptr) 0, message_size))
  967.         bfd_fatal ("message section");
  968.       nlm_extended_header (outbfd)->messageFileOffset =
  969.         message_section->filepos;
  970.       nlm_extended_header (outbfd)->messageFileLength = message_size;
  971.  
  972.       /* FIXME: Are these offsets correct on all platforms?  Are
  973.          they 32 bits on all platforms?  What endianness?  */
  974.       nlm_extended_header (outbfd)->languageID =
  975.         bfd_h_get_32 (outbfd, (bfd_byte *) data + 106);
  976.       nlm_extended_header (outbfd)->messageCount =
  977.         bfd_h_get_32 (outbfd, (bfd_byte *) data + 110);
  978.     }
  979.       free (data);
  980.     }
  981.   if (modules != NULL)
  982.     {
  983.       PTR data;
  984.       unsigned char *set;
  985.       struct string_list *l;
  986.       bfd_size_type c;
  987.  
  988.       data = xmalloc (module_size);
  989.       c = 0;
  990.       set = (unsigned char *) data;
  991.       for (l = modules; l != NULL; l = l->next)
  992.     {
  993.       *set = strlen (l->string);
  994.       strncpy (set + 1, l->string, *set);
  995.       set += *set + 1;
  996.       ++c;
  997.     }
  998.       if (! bfd_set_section_contents (outbfd, module_section, data,
  999.                       (file_ptr) 0, module_size))
  1000.     bfd_fatal ("module section");
  1001.       nlm_fixed_header (outbfd)->moduleDependencyOffset =
  1002.     module_section->filepos;
  1003.       nlm_fixed_header (outbfd)->numberOfModuleDependencies = c;
  1004.     }
  1005.   if (rpc_file != NULL)
  1006.     {
  1007.       PTR data;
  1008.  
  1009.       data = xmalloc (rpc_size);
  1010.       if (fread (data, 1, rpc_size, rpc_data) != rpc_size)
  1011.     fprintf (stderr, "%s:%s: read: %s\n", program_name, rpc_file,
  1012.          strerror (errno));
  1013.       else
  1014.     {
  1015.       if (! bfd_set_section_contents (outbfd, rpc_section, data,
  1016.                       (file_ptr) 0, rpc_size))
  1017.         bfd_fatal ("rpc section");
  1018.       nlm_extended_header (outbfd)->RPCDataOffset =
  1019.         rpc_section->filepos;
  1020.       nlm_extended_header (outbfd)->RPCDataLength = rpc_size;
  1021.     }
  1022.       free (data);
  1023.     }
  1024.   if (sharelib_file != NULL)
  1025.     {
  1026.       PTR data;
  1027.  
  1028.       data = xmalloc (shared_size);
  1029.       if (fseek (shared_data, shared_offset, SEEK_SET) != 0
  1030.       || fread (data, 1, shared_size, shared_data) != shared_size)
  1031.     fprintf (stderr, "%s:%s: read: %s\n", program_name, sharelib_file,
  1032.          strerror (errno));
  1033.       else
  1034.     {
  1035.       if (! bfd_set_section_contents (outbfd, shared_section, data,
  1036.                       (file_ptr) 0, shared_size))
  1037.         bfd_fatal ("shared section");
  1038.     }
  1039.       nlm_extended_header (outbfd)->sharedCodeOffset =
  1040.     sharedhdr.codeImageOffset - shared_offset + shared_section->filepos;
  1041.       nlm_extended_header (outbfd)->sharedCodeLength =
  1042.     sharedhdr.codeImageSize;
  1043.       nlm_extended_header (outbfd)->sharedDataOffset =
  1044.     sharedhdr.dataImageOffset - shared_offset + shared_section->filepos;
  1045.       nlm_extended_header (outbfd)->sharedDataLength =
  1046.     sharedhdr.dataImageSize;
  1047.       nlm_extended_header (outbfd)->sharedRelocationFixupOffset =
  1048.     (sharedhdr.relocationFixupOffset
  1049.      - shared_offset
  1050.      + shared_section->filepos);
  1051.       nlm_extended_header (outbfd)->sharedRelocationFixupCount =
  1052.     sharedhdr.numberOfRelocationFixups;
  1053.       nlm_extended_header (outbfd)->sharedExternalReferenceOffset =
  1054.     (sharedhdr.externalReferencesOffset
  1055.      - shared_offset
  1056.      + shared_section->filepos);
  1057.       nlm_extended_header (outbfd)->sharedExternalReferenceCount =
  1058.     sharedhdr.numberOfExternalReferences;
  1059.       nlm_extended_header (outbfd)->sharedPublicsOffset =
  1060.     sharedhdr.publicsOffset - shared_offset + shared_section->filepos;
  1061.       nlm_extended_header (outbfd)->sharedPublicsCount =
  1062.     sharedhdr.numberOfPublics;
  1063.       nlm_extended_header (outbfd)->sharedDebugRecordOffset =
  1064.     sharedhdr.debugInfoOffset - shared_offset + shared_section->filepos;
  1065.       nlm_extended_header (outbfd)->sharedDebugRecordCount =
  1066.     sharedhdr.numberOfDebugRecords;
  1067.       nlm_extended_header (outbfd)->SharedInitializationOffset =
  1068.     sharedhdr.codeStartOffset;
  1069.       nlm_extended_header (outbfd)->SharedExitProcedureOffset =
  1070.     sharedhdr.exitProcedureOffset;
  1071.       free (data);
  1072.     }
  1073.   len = strlen (output_file);
  1074.   if (len > NLM_MODULE_NAME_SIZE - 2)
  1075.     len = NLM_MODULE_NAME_SIZE - 2;
  1076.   nlm_fixed_header (outbfd)->moduleName[0] = len;
  1077.  
  1078.   strncpy (nlm_fixed_header (outbfd)->moduleName + 1, output_file,
  1079.        NLM_MODULE_NAME_SIZE - 2);
  1080.   nlm_fixed_header (outbfd)->moduleName[NLM_MODULE_NAME_SIZE - 1] = '\0';
  1081.   for (modname = nlm_fixed_header (outbfd)->moduleName;
  1082.        *modname != '\0';
  1083.        modname++)
  1084.     if (islower (*modname))
  1085.       *modname = toupper (*modname);
  1086.  
  1087.   strncpy (nlm_variable_header (outbfd)->oldThreadName, " LONG",
  1088.        NLM_OLD_THREAD_NAME_LENGTH);
  1089.  
  1090.   nlm_cygnus_ext_header (outbfd)->offset = secsec->filepos;
  1091.   nlm_cygnus_ext_header (outbfd)->length = bfd_section_size (outbfd, secsec);
  1092.  
  1093.   if (! bfd_close (outbfd))
  1094.     bfd_fatal (output_file);
  1095.   if (! bfd_close (inbfd))
  1096.     bfd_fatal (input_file);
  1097.  
  1098.   if (unlink_on_exit != NULL)
  1099.     unlink (unlink_on_exit);
  1100.  
  1101.   return 0;
  1102. }
  1103.  
  1104. /* Display a help message and exit.  */
  1105.  
  1106. static void
  1107. show_help ()
  1108. {
  1109.   printf ("%s: Convert an object file into a NetWare Loadable Module\n",
  1110.       program_name);
  1111.   show_usage (stdout, 0);
  1112. }
  1113.  
  1114. /* Show a usage message and exit.  */
  1115.  
  1116. static void
  1117. show_usage (file, status)
  1118.      FILE *file;
  1119.      int status;
  1120. {
  1121.   fprintf (file, "\
  1122. Usage: %s [-dhV] [-I bfdname] [-O bfdname] [-T header-file] [-l linker]\n\
  1123.        [--input-target=bfdname] [--output-target=bfdname]\n\
  1124.        [--header-file=file] [--linker=linker] [--debug]\n\
  1125.        [--help] [--version]\n\
  1126.        [in-file [out-file]]\n",
  1127.        program_name);
  1128.   exit (status);
  1129. }
  1130.  
  1131. /* Select the output format based on the input architecture, machine,
  1132.    and endianness.  This chooses the appropriate NLM target.  */
  1133.  
  1134. static const char *
  1135. select_output_format (arch, mach, bigendian)
  1136.      enum bfd_architecture arch;
  1137.      unsigned long mach;
  1138.      boolean bigendian;
  1139. {
  1140.   switch (arch)
  1141.     {
  1142. #ifdef NLMCONV_I386
  1143.     case bfd_arch_i386:
  1144.       return "nlm32-i386";
  1145. #endif
  1146. #ifdef NLMCONV_SPARC
  1147.     case bfd_arch_sparc:
  1148.       return "nlm32-sparc";
  1149. #endif
  1150. #ifdef NLMCONV_ALPHA
  1151.     case bfd_arch_alpha:
  1152.       return "nlm32-alpha";
  1153. #endif
  1154. #ifdef NLMCONV_POWERPC
  1155.     case bfd_arch_powerpc:
  1156.       return "nlm32-powerpc";
  1157. #endif
  1158.     default:
  1159.       fprintf (stderr, "%s: support not compiled in for %s\n",
  1160.            program_name, bfd_printable_arch_mach (arch, mach));
  1161.       exit (1);
  1162.       /* Avoid warning.  */
  1163.       return NULL;
  1164.     }
  1165.   /*NOTREACHED*/
  1166. }
  1167.  
  1168. /* The BFD sections are copied in two passes.  This function selects
  1169.    the output section for each input section, and sets up the section
  1170.    name, size, etc.  */
  1171.  
  1172. static void
  1173. setup_sections (inbfd, insec, data_ptr)
  1174.      bfd *inbfd;
  1175.      asection *insec;
  1176.      PTR data_ptr;
  1177. {
  1178.   bfd *outbfd = (bfd *) data_ptr;
  1179.   flagword f;
  1180.   const char *outname;
  1181.   asection *outsec;
  1182.   bfd_vma offset;
  1183.   bfd_size_type align;
  1184.   bfd_size_type add;
  1185.   bfd_size_type secsecsize;
  1186.  
  1187.   f = bfd_get_section_flags (inbfd, insec);
  1188.   if (f & SEC_CODE)
  1189.     outname = NLM_CODE_NAME;
  1190.   else if ((f & SEC_LOAD) && (f & SEC_HAS_CONTENTS))
  1191.     outname = NLM_INITIALIZED_DATA_NAME;
  1192.   else if (f & SEC_ALLOC)
  1193.     outname = NLM_UNINITIALIZED_DATA_NAME;
  1194.   else
  1195.     outname = bfd_section_name (inbfd, insec);
  1196.  
  1197.   outsec = bfd_get_section_by_name (outbfd, outname);
  1198.   if (outsec == NULL)
  1199.     {
  1200.       outsec = bfd_make_section (outbfd, outname);
  1201.       if (outsec == NULL)
  1202.     bfd_fatal ("make section");
  1203.     }
  1204.  
  1205.   insec->output_section = outsec;
  1206.  
  1207.   offset = bfd_section_size (outbfd, outsec);
  1208.   align = 1 << bfd_section_alignment (inbfd, insec);
  1209.   add = ((offset + align - 1) &~ (align - 1)) - offset;
  1210.   insec->output_offset = offset + add;
  1211.  
  1212.   if (! bfd_set_section_size (outbfd, outsec,
  1213.                   (bfd_section_size (outbfd, outsec)
  1214.                    + bfd_section_size (inbfd, insec)
  1215.                    + add)))
  1216.     bfd_fatal ("set section size");
  1217.  
  1218.   if ((bfd_section_alignment (inbfd, insec)
  1219.        > bfd_section_alignment (outbfd, outsec))
  1220.       && ! bfd_set_section_alignment (outbfd, outsec,
  1221.                       bfd_section_alignment (inbfd, insec)))
  1222.     bfd_fatal ("set section alignment");
  1223.  
  1224.   if (! bfd_set_section_flags (outbfd, outsec,
  1225.                    f | bfd_get_section_flags (outbfd, outsec)))
  1226.     bfd_fatal ("set section flags");
  1227.  
  1228.   bfd_set_reloc (outbfd, outsec, (arelent **) NULL, 0);
  1229.  
  1230.   /* For each input section we allocate space for an entry in
  1231.      .nlmsections.  */
  1232.   secsecsize = bfd_section_size (outbfd, secsec);
  1233.   secsecsize += strlen (bfd_section_name (inbfd, insec)) + 1;
  1234.   secsecsize = (secsecsize + 3) &~ 3;
  1235.   secsecsize += 8;
  1236.   if (! bfd_set_section_size (outbfd, secsec, secsecsize))
  1237.     bfd_fatal ("set .nlmsections size");
  1238. }
  1239.  
  1240. /* Copy the section contents.  */
  1241.  
  1242. static void
  1243. copy_sections (inbfd, insec, data_ptr)
  1244.      bfd *inbfd;
  1245.      asection *insec;
  1246.      PTR data_ptr;
  1247. {
  1248.   static bfd_size_type secsecoff = 0;
  1249.   bfd *outbfd = (bfd *) data_ptr;
  1250.   const char *inname;
  1251.   asection *outsec;
  1252.   bfd_size_type size;
  1253.   PTR contents;
  1254.   long reloc_size;
  1255.   bfd_byte buf[4];
  1256.   bfd_size_type add;
  1257.  
  1258.   inname = bfd_section_name (inbfd, insec);
  1259.  
  1260.   outsec = insec->output_section;
  1261.   assert (outsec != NULL);
  1262.  
  1263.   size = bfd_get_section_size_before_reloc (insec);
  1264.  
  1265.   /* FIXME: Why are these necessary?  */
  1266.   insec->_cooked_size = insec->_raw_size;
  1267.   insec->reloc_done = true;
  1268.  
  1269.   if ((bfd_get_section_flags (inbfd, insec) & SEC_HAS_CONTENTS) == 0)
  1270.     contents = NULL;
  1271.   else
  1272.     {
  1273.       contents = xmalloc (size);
  1274.       if (! bfd_get_section_contents (inbfd, insec, contents,
  1275.                       (file_ptr) 0, size))
  1276.     bfd_fatal (bfd_get_filename (inbfd));
  1277.     }
  1278.  
  1279.   reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
  1280.   if (reloc_size < 0)
  1281.     bfd_fatal (bfd_get_filename (inbfd));
  1282.   if (reloc_size != 0)
  1283.     {
  1284.       arelent **relocs;
  1285.       long reloc_count;
  1286.  
  1287.       relocs = (arelent **) xmalloc (reloc_size);
  1288.       reloc_count = bfd_canonicalize_reloc (inbfd, insec, relocs, symbols);
  1289.       if (reloc_count < 0)
  1290.     bfd_fatal (bfd_get_filename (inbfd));
  1291.       mangle_relocs (outbfd, insec, &relocs, &reloc_count, (char *) contents,
  1292.              size);
  1293.  
  1294.       /* FIXME: refers to internal BFD fields.  */
  1295.       if (outsec->orelocation != (arelent **) NULL)
  1296.     {
  1297.       bfd_size_type total_count;
  1298.       arelent **combined;
  1299.  
  1300.       total_count = reloc_count + outsec->reloc_count;
  1301.       combined = (arelent **) xmalloc (total_count * sizeof (arelent *));
  1302.       memcpy (combined, outsec->orelocation,
  1303.           outsec->reloc_count * sizeof (arelent *));
  1304.       memcpy (combined + outsec->reloc_count, relocs,
  1305.           (size_t) (reloc_count * sizeof (arelent *)));
  1306.       free (outsec->orelocation);
  1307.       reloc_count = total_count;
  1308.       relocs = combined;
  1309.     }
  1310.  
  1311.       bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
  1312.     }
  1313.  
  1314.   if (contents != NULL)
  1315.     {
  1316.       if (! bfd_set_section_contents (outbfd, outsec, contents,
  1317.                       insec->output_offset, size))
  1318.     bfd_fatal (bfd_get_filename (outbfd));
  1319.       free (contents);
  1320.     }
  1321.  
  1322.   /* Add this section to .nlmsections.  */
  1323.   if (! bfd_set_section_contents (outbfd, secsec, (PTR) inname, secsecoff,
  1324.                   strlen (inname) + 1))
  1325.     bfd_fatal ("set .nlmsection contents");
  1326.   secsecoff += strlen (inname) + 1;
  1327.  
  1328.   add = ((secsecoff + 3) &~ 3) - secsecoff;
  1329.   if (add != 0)
  1330.     {
  1331.       bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
  1332.       if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, add))
  1333.     bfd_fatal ("set .nlmsection contents");
  1334.       secsecoff += add;
  1335.     }
  1336.  
  1337.   if (contents != NULL)
  1338.     bfd_h_put_32 (outbfd, (bfd_vma) outsec->filepos, buf);
  1339.   else
  1340.     bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
  1341.   if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
  1342.     bfd_fatal ("set .nlmsection contents");
  1343.   secsecoff += 4;
  1344.  
  1345.   bfd_h_put_32 (outbfd, (bfd_vma) size, buf);
  1346.   if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
  1347.     bfd_fatal ("set .nlmsection contents");
  1348.   secsecoff += 4;
  1349. }
  1350.  
  1351. /* Some, perhaps all, NetWare targets require changing the relocs used
  1352.    by the input formats.  */
  1353.  
  1354. static void
  1355. mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
  1356.            contents_size)
  1357.      bfd *outbfd;
  1358.      asection *insec;
  1359.      arelent ***relocs_ptr;
  1360.      long *reloc_count_ptr;
  1361.      char *contents;
  1362.      bfd_size_type contents_size;
  1363. {
  1364.   switch (bfd_get_arch (outbfd))
  1365.     {
  1366. #ifdef NLMCONV_I386
  1367.     case bfd_arch_i386:
  1368.       i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1369.               contents, contents_size);
  1370.       break;
  1371. #endif
  1372. #ifdef NLMCONV_ALPHA
  1373.     case bfd_arch_alpha:
  1374.       alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1375.                contents, contents_size);
  1376.       break;
  1377. #endif
  1378. #ifdef NLMCONV_POWERPC
  1379.     case bfd_arch_powerpc:
  1380.       powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1381.                  contents, contents_size);
  1382.       break;
  1383. #endif
  1384.     default:
  1385.       default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
  1386.                  contents, contents_size);
  1387.       break;
  1388.     }
  1389. }
  1390.  
  1391. /* By default all we need to do for relocs is change the address by
  1392.    the output_offset.  */
  1393.  
  1394. /*ARGSUSED*/
  1395. static void
  1396. default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
  1397.                contents_size)
  1398.      bfd *outbfd;
  1399.      asection *insec;
  1400.      arelent ***relocs_ptr;
  1401.      long *reloc_count_ptr;
  1402.      char *contents;
  1403.      bfd_size_type contents_size;
  1404. {
  1405.   if (insec->output_offset != 0)
  1406.     {
  1407.       long reloc_count;
  1408.       register arelent **relocs;
  1409.       register long i;
  1410.  
  1411.       reloc_count = *reloc_count_ptr;
  1412.       relocs = *relocs_ptr;
  1413.       for (i = 0; i < reloc_count; i++, relocs++)
  1414.     (*relocs)->address += insec->output_offset;
  1415.     }
  1416. }
  1417.  
  1418. #ifdef NLMCONV_I386
  1419.  
  1420. /* NetWare on the i386 supports a restricted set of relocs, which are
  1421.    different from those used on other i386 targets.  This routine
  1422.    converts the relocs.  It is, obviously, very target dependent.  At
  1423.    the moment, the nlm32-i386 backend performs similar translations;
  1424.    however, it is more reliable and efficient to do them here.  */
  1425.  
  1426. static reloc_howto_type nlm_i386_pcrel_howto =
  1427.   HOWTO (1,            /* type */
  1428.      0,            /* rightshift */
  1429.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1430.      32,            /* bitsize */
  1431.      true,            /* pc_relative */
  1432.      0,            /* bitpos */
  1433.      complain_overflow_signed, /* complain_on_overflow */
  1434.      0,            /* special_function */
  1435.      "DISP32",        /* name */
  1436.      true,            /* partial_inplace */
  1437.      0xffffffff,        /* src_mask */
  1438.      0xffffffff,        /* dst_mask */
  1439.      true);            /* pcrel_offset */
  1440.  
  1441. static void
  1442. i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
  1443.             contents_size)
  1444.      bfd *outbfd;
  1445.      asection *insec;
  1446.      arelent ***relocs_ptr;
  1447.      long *reloc_count_ptr;
  1448.      char *contents;
  1449.      bfd_size_type contents_size;
  1450. {
  1451.   long reloc_count, i;
  1452.   arelent **relocs;
  1453.  
  1454.   reloc_count = *reloc_count_ptr;
  1455.   relocs = *relocs_ptr;
  1456.   for (i = 0; i < reloc_count; i++)
  1457.     {
  1458.       arelent *rel;
  1459.       asymbol *sym;
  1460.       bfd_size_type address;
  1461.       bfd_vma addend;
  1462.  
  1463.       rel = *relocs++;
  1464.       sym = *rel->sym_ptr_ptr;
  1465.  
  1466.       /* We're moving the relocs from the input section to the output
  1467.      section, so we must adjust the address accordingly.  */
  1468.       address = rel->address;
  1469.       rel->address += insec->output_offset;
  1470.  
  1471.       /* Note that no serious harm will ensue if we fail to change a
  1472.      reloc.  The backend will fail when writing out the reloc.  */
  1473.  
  1474.       /* Make sure this reloc is within the data we have.  We use only
  1475.      4 byte relocs here, so we insist on having 4 bytes.  */
  1476.       if (address + 4 > contents_size)
  1477.     continue;
  1478.  
  1479.       /* A PC relative reloc entirely within a single section is
  1480.      completely unnecessary.  This can be generated by ld -r.  */
  1481.       if (sym == insec->symbol
  1482.       && rel->howto != NULL
  1483.       && rel->howto->pc_relative
  1484.       && ! rel->howto->pcrel_offset)
  1485.     {
  1486.       --*reloc_count_ptr;
  1487.       --relocs;
  1488.       memmove (relocs, relocs + 1,
  1489.            (size_t) ((reloc_count - i) * sizeof (arelent *)));
  1490.       continue;
  1491.     }
  1492.  
  1493.       /* Get the amount the relocation will add in.  */
  1494.       addend = rel->addend + sym->value;
  1495.  
  1496.       /* NetWare doesn't support PC relative relocs against defined
  1497.      symbols, so we have to eliminate them by doing the relocation
  1498.      now.  We can only do this if the reloc is within a single
  1499.      section.  */
  1500.       if (rel->howto != NULL
  1501.       && rel->howto->pc_relative
  1502.       && bfd_get_section (sym) == insec->output_section)
  1503.     {
  1504.       bfd_vma val;
  1505.  
  1506.       if (rel->howto->pcrel_offset)
  1507.         addend -= address;
  1508.  
  1509.       val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1510.       val += addend;
  1511.       bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1512.  
  1513.       --*reloc_count_ptr;
  1514.       --relocs;
  1515.       memmove (relocs, relocs + 1,
  1516.            (size_t) ((reloc_count - i) * sizeof (arelent *)));
  1517.       continue;
  1518.     }
  1519.  
  1520.       /* NetWare doesn't support reloc addends, so we get rid of them
  1521.      here by simply adding them into the object data.  We handle
  1522.      the symbol value, if any, the same way.  */
  1523.       if (addend != 0
  1524.       && rel->howto != NULL
  1525.       && rel->howto->rightshift == 0
  1526.       && rel->howto->size == 2
  1527.       && rel->howto->bitsize == 32
  1528.       && rel->howto->bitpos == 0
  1529.       && rel->howto->src_mask == 0xffffffff
  1530.       && rel->howto->dst_mask == 0xffffffff)
  1531.     {
  1532.       bfd_vma val;
  1533.  
  1534.       val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1535.       val += addend;
  1536.       bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1537.  
  1538.       /* Adjust the reloc for the changes we just made.  */
  1539.       rel->addend = 0;
  1540.       if (! bfd_is_und_section (bfd_get_section (sym)))
  1541.         rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
  1542.     }
  1543.  
  1544.       /* NetWare uses a reloc with pcrel_offset set.  We adjust
  1545.      pc_relative relocs accordingly.  We are going to change the
  1546.      howto field, so we can only do this if the current one is
  1547.      compatible.  We should check that special_function is NULL
  1548.      here, but at the moment coff-i386 uses a special_function
  1549.      which does not affect what we are doing here.  */
  1550.       if (rel->howto != NULL
  1551.       && rel->howto->pc_relative
  1552.       && ! rel->howto->pcrel_offset
  1553.       && rel->howto->rightshift == 0
  1554.       && rel->howto->size == 2
  1555.       && rel->howto->bitsize == 32
  1556.       && rel->howto->bitpos == 0
  1557.       && rel->howto->src_mask == 0xffffffff
  1558.       && rel->howto->dst_mask == 0xffffffff)
  1559.     {
  1560.       bfd_vma val;
  1561.  
  1562.       /* When pcrel_offset is not set, it means that the negative
  1563.          of the address of the memory location is stored in the
  1564.          memory location.  We must add it back in.  */
  1565.       val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
  1566.       val += address;
  1567.       bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
  1568.  
  1569.       /* We must change to a new howto.  */
  1570.       rel->howto = &nlm_i386_pcrel_howto;
  1571.     }
  1572.     }
  1573. }
  1574.  
  1575. #endif /* NLMCONV_I386 */
  1576.  
  1577. #ifdef NLMCONV_ALPHA
  1578.  
  1579. /* On the Alpha the first reloc for every section must be a special
  1580.    relocs which hold the GP address.  Also, the first reloc in the
  1581.    file must be a special reloc which holds the address of the .lita
  1582.    section.  */
  1583.  
  1584. static reloc_howto_type nlm32_alpha_nw_howto =
  1585.   HOWTO (ALPHA_R_NW_RELOC,    /* type */
  1586.      0,            /* rightshift */
  1587.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  1588.      0,            /* bitsize */
  1589.      false,            /* pc_relative */
  1590.      0,            /* bitpos */
  1591.      complain_overflow_dont, /* complain_on_overflow */
  1592.      0,            /* special_function */
  1593.      "NW_RELOC",        /* name */
  1594.      false,            /* partial_inplace */
  1595.      0,            /* src_mask */
  1596.      0,            /* dst_mask */
  1597.      false);        /* pcrel_offset */
  1598.  
  1599. /*ARGSUSED*/
  1600. static void
  1601. alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
  1602.              contents_size)
  1603.      bfd *outbfd;
  1604.      asection *insec;
  1605.      register arelent ***relocs_ptr;
  1606.      long *reloc_count_ptr;
  1607.      char *contents;
  1608.      bfd_size_type contents_size;
  1609. {
  1610.   long old_reloc_count;
  1611.   arelent **old_relocs;
  1612.   register arelent **relocs;
  1613.  
  1614.   old_reloc_count = *reloc_count_ptr;
  1615.   old_relocs = *relocs_ptr;
  1616.   relocs = (arelent **) xmalloc ((old_reloc_count + 3) * sizeof (arelent *));
  1617.   *relocs_ptr = relocs;
  1618.  
  1619.   if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
  1620.     {
  1621.       bfd *inbfd;
  1622.       asection *lita_section;
  1623.  
  1624.       inbfd = insec->owner;
  1625.       lita_section = bfd_get_section_by_name (inbfd, _LITA);
  1626.       if (lita_section != (asection *) NULL)
  1627.     {
  1628.       nlm_alpha_backend_data (outbfd)->lita_address =
  1629.         bfd_get_section_vma (inbfd, lita_section);
  1630.       nlm_alpha_backend_data (outbfd)->lita_size =
  1631.         bfd_section_size (inbfd, lita_section);
  1632.     }
  1633.       else
  1634.     {
  1635.       /* Avoid outputting this reloc again.  */
  1636.       nlm_alpha_backend_data (outbfd)->lita_address = 4;
  1637.     }
  1638.  
  1639.       *relocs = (arelent *) xmalloc (sizeof (arelent));
  1640.       (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1641.       (*relocs)->address = nlm_alpha_backend_data (outbfd)->lita_address;
  1642.       (*relocs)->addend = nlm_alpha_backend_data (outbfd)->lita_size + 1;
  1643.       (*relocs)->howto = &nlm32_alpha_nw_howto;
  1644.       ++relocs;
  1645.       ++(*reloc_count_ptr);
  1646.     }
  1647.  
  1648.   /* Get the GP value from bfd.  */
  1649.   if (nlm_alpha_backend_data (outbfd)->gp == 0)
  1650.     nlm_alpha_backend_data (outbfd)->gp =
  1651.       bfd_ecoff_get_gp_value (insec->owner);
  1652.  
  1653.   *relocs = (arelent *) xmalloc (sizeof (arelent));
  1654.   (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  1655.   (*relocs)->address = nlm_alpha_backend_data (outbfd)->gp;
  1656.   (*relocs)->addend = 0;
  1657.   (*relocs)->howto = &nlm32_alpha_nw_howto;
  1658.   ++relocs;
  1659.   ++(*reloc_count_ptr);
  1660.  
  1661.   memcpy ((PTR) relocs, (PTR) old_relocs,
  1662.       (size_t) old_reloc_count * sizeof (arelent *));
  1663.   relocs[old_reloc_count] = (arelent *) NULL;
  1664.  
  1665.   free (old_relocs);
  1666.  
  1667.   if (insec->output_offset != 0)
  1668.     {
  1669.       register bfd_size_type i;
  1670.  
  1671.       for (i = 0; i < old_reloc_count; i++, relocs++)
  1672.     (*relocs)->address += insec->output_offset;
  1673.     }
  1674. }
  1675.  
  1676. #endif /* NLMCONV_ALPHA */
  1677.  
  1678. #ifdef NLMCONV_POWERPC
  1679.  
  1680. /* We keep a linked list of stubs which we must build.  Because BFD
  1681.    requires us to know the sizes of all sections before we can set the
  1682.    contents of any, we must figure out which stubs we want to build
  1683.    before we can actually build any of them.  */
  1684.  
  1685. struct powerpc_stub
  1686. {
  1687.   /* Next stub in linked list.  */
  1688.   struct powerpc_stub *next;
  1689.  
  1690.   /* Symbol whose value is the start of the stub.  This is a symbol
  1691.      whose name begins with `.'.  */
  1692.   asymbol *start;
  1693.  
  1694.   /* Symbol we are going to create a reloc against.  This is a symbol
  1695.      with the same name as START but without the leading `.'.  */
  1696.   asymbol *reloc;
  1697.  
  1698.   /* The TOC index for this stub.  This is the index into the TOC
  1699.      section at which the reloc is created.  */
  1700.   unsigned int toc_index;
  1701. };
  1702.  
  1703. /* The linked list of stubs.  */
  1704.  
  1705. static struct powerpc_stub *powerpc_stubs;
  1706.  
  1707. /* This is what a stub looks like.  The first instruction will get
  1708.    adjusted with the correct TOC index.  */
  1709.  
  1710. static unsigned long powerpc_stub_insns[] =
  1711. {
  1712.   0x81820000,        /* lwz     r12,0(r2) */
  1713.   0x90410014,        /* stw     r2,20(r1) */
  1714.   0x800c0000,        /* lwz     r0,0(r12) */
  1715.   0x804c0004,        /* lwz     r2,r(r12) */
  1716.   0x7c0903a6,        /* mtctr r0 */
  1717.   0x4e800420,        /* bctr */
  1718.   0,            /* Traceback table.  */
  1719.   0xc8000,
  1720.   0
  1721. };
  1722.  
  1723. #define POWERPC_STUB_INSN_COUNT \
  1724.   (sizeof powerpc_stub_insns / sizeof powerpc_stub_insns[0])
  1725.  
  1726. #define POWERPC_STUB_SIZE (4 * POWERPC_STUB_INSN_COUNT)
  1727.  
  1728. /* Each stub uses a four byte TOC entry.  */
  1729. #define POWERPC_STUB_TOC_ENTRY_SIZE (4)
  1730.  
  1731. /* The original size of the .got section.  */
  1732. static bfd_size_type powerpc_initial_got_size;
  1733.  
  1734. /* Look for all undefined symbols beginning with `.', and prepare to
  1735.    build a stub for each one.  */
  1736.  
  1737. static void
  1738. powerpc_build_stubs (inbfd, outbfd, symbols_ptr, symcount_ptr)
  1739.      bfd *inbfd;
  1740.      bfd *outbfd;
  1741.      asymbol ***symbols_ptr;
  1742.      long *symcount_ptr;
  1743. {
  1744.   asection *stub_sec;
  1745.   asection *got_sec;
  1746.   unsigned int got_base;
  1747.   long i;
  1748.   long symcount;
  1749.   long stubcount;
  1750.  
  1751.   /* Make a section to hold stubs.  We don't set SEC_HAS_CONTENTS for
  1752.      the section to prevent copy_sections from reading from it.  */
  1753.   stub_sec = bfd_make_section (inbfd, ".stubs");
  1754.   if (stub_sec == (asection *) NULL
  1755.       || ! bfd_set_section_flags (inbfd, stub_sec,
  1756.                   (SEC_CODE
  1757.                    | SEC_RELOC
  1758.                    | SEC_ALLOC
  1759.                    | SEC_LOAD))
  1760.       || ! bfd_set_section_alignment (inbfd, stub_sec, 2))
  1761.     bfd_fatal (".stubs");
  1762.  
  1763.   /* Get the TOC section, which is named .got.  */
  1764.   got_sec = bfd_get_section_by_name (inbfd, ".got");
  1765.   if (got_sec == (asection *) NULL)
  1766.     {
  1767.       got_sec = bfd_make_section (inbfd, ".got");
  1768.       if (got_sec == (asection *) NULL
  1769.       || ! bfd_set_section_flags (inbfd, got_sec,
  1770.                       (SEC_DATA
  1771.                        | SEC_RELOC
  1772.                        | SEC_ALLOC
  1773.                        | SEC_LOAD
  1774.                        | SEC_HAS_CONTENTS))
  1775.       || ! bfd_set_section_alignment (inbfd, got_sec, 2))
  1776.     bfd_fatal (".got");
  1777.     }
  1778.  
  1779.   powerpc_initial_got_size = bfd_section_size (inbfd, got_sec);
  1780.   got_base = powerpc_initial_got_size;
  1781.   got_base = (got_base + 3) &~ 3;
  1782.  
  1783.   stubcount = 0;
  1784.  
  1785.   symcount = *symcount_ptr;
  1786.   for (i = 0; i < symcount; i++)
  1787.     {
  1788.       asymbol *sym;
  1789.       asymbol *newsym;
  1790.       char *newname;
  1791.       struct powerpc_stub *item;
  1792.  
  1793.       sym = (*symbols_ptr)[i];
  1794.  
  1795.       /* We must make a stub for every undefined symbol whose name
  1796.      starts with '.'.  */
  1797.       if (bfd_asymbol_name (sym)[0] != '.'
  1798.       || ! bfd_is_und_section (bfd_get_section (sym)))
  1799.     continue;
  1800.  
  1801.       /* Make a new undefined symbol with the same name but without
  1802.      the leading `.'.  */
  1803.       newsym = (asymbol *) xmalloc (sizeof (asymbol));
  1804.       *newsym = *sym;
  1805.       newname = (char *) xmalloc (strlen (bfd_asymbol_name (sym)));
  1806.       strcpy (newname, bfd_asymbol_name (sym) + 1);
  1807.       newsym->name = newname;
  1808.  
  1809.       /* Define the `.' symbol to be in the stub section.  */
  1810.       sym->section = stub_sec;
  1811.       sym->value = stubcount * POWERPC_STUB_SIZE;
  1812.       /* We set the BSF_DYNAMIC flag here so that we can check it when
  1813.      we are mangling relocs.  FIXME: This is a hack.  */
  1814.       sym->flags = BSF_LOCAL | BSF_DYNAMIC;
  1815.  
  1816.       /* Add this stub to the linked list.  */
  1817.       item = (struct powerpc_stub *) xmalloc (sizeof (struct powerpc_stub));
  1818.       item->start = sym;
  1819.       item->reloc = newsym;
  1820.       item->toc_index = got_base + stubcount * POWERPC_STUB_TOC_ENTRY_SIZE;
  1821.  
  1822.       item->next = powerpc_stubs;
  1823.       powerpc_stubs = item;
  1824.       
  1825.       ++stubcount;
  1826.     }
  1827.  
  1828.   if (stubcount > 0)
  1829.     {
  1830.       asymbol **s;
  1831.       struct powerpc_stub *l;
  1832.  
  1833.       /* Add the new symbols we just created to the symbol table.  */
  1834.       *symbols_ptr = (asymbol **) xrealloc ((char *) *symbols_ptr,
  1835.                         ((symcount + stubcount)
  1836.                          * sizeof (asymbol)));
  1837.       *symcount_ptr += stubcount;
  1838.       s = &(*symbols_ptr)[symcount];
  1839.       for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1840.     *s++ = l->reloc;
  1841.  
  1842.       /* Set the size of the .stubs section and increase the size of
  1843.      the .got section.  */
  1844.       if (! bfd_set_section_size (inbfd, stub_sec,
  1845.                   stubcount * POWERPC_STUB_SIZE)
  1846.       || ! bfd_set_section_size (inbfd, got_sec,
  1847.                      (got_base
  1848.                       + (stubcount
  1849.                      * POWERPC_STUB_TOC_ENTRY_SIZE))))
  1850.     bfd_fatal ("stub section sizes");
  1851.     }
  1852. }
  1853.  
  1854. /* Resolve all the stubs for PowerPC NetWare.  We fill in the contents
  1855.    of the output section, and create new relocs in the TOC.  */
  1856.  
  1857. static void
  1858. powerpc_resolve_stubs (inbfd, outbfd)
  1859.      bfd *inbfd;
  1860.      bfd *outbfd;
  1861. {
  1862.   bfd_byte buf[POWERPC_STUB_SIZE];
  1863.   unsigned int i;
  1864.   unsigned int stubcount;
  1865.   arelent **relocs;
  1866.   asection *got_sec;
  1867.   arelent **r;
  1868.   struct powerpc_stub *l;
  1869.  
  1870.   if (powerpc_stubs == (struct powerpc_stub *) NULL)
  1871.     return;
  1872.  
  1873.   for (i = 0; i < POWERPC_STUB_INSN_COUNT; i++)
  1874.     bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[i], buf + i * 4);
  1875.  
  1876.   got_sec = bfd_get_section_by_name (inbfd, ".got");
  1877.   assert (got_sec != (asection *) NULL);
  1878.   assert (got_sec->output_section->orelocation == (arelent **) NULL);
  1879.  
  1880.   stubcount = 0;
  1881.   for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1882.     ++stubcount;
  1883.   relocs = (arelent **) xmalloc (stubcount * sizeof (arelent *));
  1884.  
  1885.   r = relocs;
  1886.   for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
  1887.     {
  1888.       arelent *reloc;
  1889.  
  1890.       /* Adjust the first instruction to use the right TOC index.  */
  1891.       bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[0] + l->toc_index, buf);
  1892.  
  1893.       /* Write this stub out.  */
  1894.       if (! bfd_set_section_contents (outbfd,
  1895.                       bfd_get_section (l->start),
  1896.                       buf,
  1897.                       l->start->value,
  1898.                       POWERPC_STUB_SIZE))
  1899.     bfd_fatal ("writing stub");
  1900.  
  1901.       /* Create a new reloc for the TOC entry.  */
  1902.       reloc = (arelent *) xmalloc (sizeof (arelent));
  1903.       reloc->sym_ptr_ptr = &l->reloc;
  1904.       reloc->address = l->toc_index + got_sec->output_offset;
  1905.       reloc->addend = 0;
  1906.       reloc->howto = bfd_reloc_type_lookup (inbfd, BFD_RELOC_32);
  1907.                       
  1908.       *r++ = reloc;
  1909.     }
  1910.  
  1911.   bfd_set_reloc (outbfd, got_sec->output_section, relocs, stubcount);
  1912. }
  1913.  
  1914. /* Adjust relocation entries for PowerPC NetWare.  We do not output
  1915.    TOC relocations.  The object code already contains the offset from
  1916.    the TOC pointer.  When the function is called, the TOC register,
  1917.    r2, will be set to the correct TOC value, so there is no need for
  1918.    any further reloc.  */
  1919.  
  1920. /*ARGSUSED*/
  1921. static void
  1922. powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
  1923.                contents_size)
  1924.      bfd *outbfd;
  1925.      asection *insec;
  1926.      register arelent ***relocs_ptr;
  1927.      long *reloc_count_ptr;
  1928.      char *contents;
  1929.      bfd_size_type contents_size;
  1930. {
  1931.   const reloc_howto_type *toc_howto;
  1932.   long reloc_count;
  1933.   register arelent **relocs;
  1934.   register long i;
  1935.  
  1936.   toc_howto = bfd_reloc_type_lookup (insec->owner, BFD_RELOC_PPC_TOC16);
  1937.   if (toc_howto == (reloc_howto_type *) NULL)
  1938.     abort ();
  1939.  
  1940.   /* If this is the .got section, clear out all the contents beyond
  1941.      the initial size.  We must do this here because copy_sections is
  1942.      going to write out whatever we return in the contents field.  */
  1943.   if (strcmp (bfd_get_section_name (insec->owner, insec), ".got") == 0)
  1944.     memset (contents + powerpc_initial_got_size, 0,
  1945.         (bfd_get_section_size_after_reloc (insec)
  1946.          - powerpc_initial_got_size));
  1947.  
  1948.   reloc_count = *reloc_count_ptr;
  1949.   relocs = *relocs_ptr;
  1950.   for (i = 0; i < reloc_count; i++)
  1951.     {
  1952.       arelent *rel;
  1953.       asymbol *sym;
  1954.       bfd_vma sym_value;
  1955.  
  1956.       rel = *relocs++;
  1957.       sym = *rel->sym_ptr_ptr;
  1958.  
  1959.       /* Convert any relocs against the .bss section into relocs
  1960.          against the .data section.  */
  1961.       if (strcmp (bfd_get_section_name (outbfd, bfd_get_section (sym)),
  1962.           NLM_UNINITIALIZED_DATA_NAME) == 0)
  1963.     {
  1964.       asection *datasec;
  1965.  
  1966.       datasec = bfd_get_section_by_name (outbfd,
  1967.                          NLM_INITIALIZED_DATA_NAME);
  1968.       if (datasec != NULL)
  1969.         {
  1970.           rel->addend += (bfd_get_section_vma (outbfd,
  1971.                            bfd_get_section (sym))
  1972.                   + sym->value);
  1973.           rel->sym_ptr_ptr = datasec->symbol_ptr_ptr;
  1974.           sym = *rel->sym_ptr_ptr;
  1975.         }
  1976.     }
  1977.  
  1978.       /* We must be able to resolve all PC relative relocs at this
  1979.      point.  If we get a branch to an undefined symbol we build a
  1980.      stub, since NetWare will resolve undefined symbols into a
  1981.      pointer to a function descriptor.  */
  1982.       if (rel->howto->pc_relative)
  1983.     {
  1984.       /* This check for whether a symbol is in the same section as
  1985.          the reloc will be wrong if there is a PC relative reloc
  1986.          between two sections both of which were placed in the
  1987.          same output section.  This should not happen.  */
  1988.       if (bfd_get_section (sym) != insec->output_section)
  1989.         fprintf (stderr, "%s: unresolved PC relative reloc against %s\n",
  1990.              program_name, bfd_asymbol_name (sym));
  1991.       else
  1992.         {
  1993.           bfd_vma val;
  1994.  
  1995.           assert (rel->howto->size == 2 && rel->howto->pcrel_offset);
  1996.           val = bfd_get_32 (outbfd, (bfd_byte *) contents + rel->address);
  1997.           val = ((val &~ rel->howto->dst_mask)
  1998.              | (((val & rel->howto->src_mask)
  1999.              + (sym->value - rel->address)
  2000.              + rel->addend)
  2001.             & rel->howto->dst_mask));
  2002.           bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
  2003.  
  2004.           /* If this reloc is against an stubbed symbol and the
  2005.          next instruction is
  2006.              cror 31,31,31
  2007.          then we replace the next instruction with
  2008.              lwz  r2,20(r1)
  2009.          This reloads the TOC pointer after a stub call.  */
  2010.           if (bfd_asymbol_name (sym)[0] == '.'
  2011.           && (sym->flags & BSF_DYNAMIC) != 0
  2012.           && (bfd_get_32 (outbfd,
  2013.                   (bfd_byte *) contents + rel->address + 4)
  2014.               == 0x4ffffb82)) /* cror 31,31,31 */
  2015.         bfd_put_32 (outbfd, (bfd_vma) 0x80410014, /* lwz r2,20(r1) */
  2016.                 (bfd_byte *) contents + rel->address + 4);
  2017.  
  2018.           --*reloc_count_ptr;
  2019.           --relocs;
  2020.           memmove (relocs, relocs + 1,
  2021.                (size_t) ((reloc_count - 1) * sizeof (arelent *)));
  2022.           continue;
  2023.         }
  2024.     }
  2025.  
  2026.       /* When considering a TOC reloc, we do not want to include the
  2027.      symbol value.  The symbol will be start of the TOC section
  2028.      (which is named .got).  We do want to include the addend.  */
  2029.       if (rel->howto == toc_howto)
  2030.     sym_value = 0;
  2031.       else
  2032.     sym_value = sym->value;
  2033.  
  2034.       /* If this is a relocation against a symbol with a value, or
  2035.      there is a reloc addend, we need to update the addend in the
  2036.      object file.  */
  2037.       if (sym_value + rel->addend != 0)
  2038.     {
  2039.       bfd_vma val;
  2040.  
  2041.       switch (rel->howto->size)
  2042.         {
  2043.         case 1:
  2044.           val = bfd_get_16 (outbfd,
  2045.                 (bfd_byte *) contents + rel->address);
  2046.           val = ((val &~ rel->howto->dst_mask)
  2047.              | (((val & rel->howto->src_mask)
  2048.              + sym_value
  2049.              + rel->addend)
  2050.             & rel->howto->dst_mask));
  2051.           if ((bfd_signed_vma) val < - 0x8000
  2052.           || (bfd_signed_vma) val >= 0x8000)
  2053.         fprintf (stderr,
  2054.              "%s: overflow when adjusting relocation against %s\n",
  2055.              program_name, bfd_asymbol_name (sym));
  2056.           bfd_put_16 (outbfd, val, (bfd_byte *) contents + rel->address);
  2057.           break;
  2058.  
  2059.         case 2:
  2060.           val = bfd_get_32 (outbfd,
  2061.                 (bfd_byte *) contents + rel->address);
  2062.           val = ((val &~ rel->howto->dst_mask)
  2063.              | (((val & rel->howto->src_mask)
  2064.              + sym_value
  2065.              + rel->addend)
  2066.             & rel->howto->dst_mask));
  2067.           bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
  2068.           break;
  2069.  
  2070.         default:
  2071.           abort ();
  2072.         }
  2073.  
  2074.       rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
  2075.       rel->addend = 0;
  2076.     }
  2077.  
  2078.       /* Now that we have incorporated the addend, remove any TOC
  2079.      relocs.  */
  2080.       if (rel->howto == toc_howto)
  2081.     {
  2082.       --*reloc_count_ptr;
  2083.       --relocs;
  2084.       memmove (relocs, relocs + 1,
  2085.            (size_t) ((reloc_count - i) * sizeof (arelent *)));
  2086.       continue;
  2087.     }
  2088.  
  2089.       rel->address += insec->output_offset;
  2090.     }
  2091. }
  2092.  
  2093. #endif /* NLMCONV_POWERPC */
  2094.  
  2095. /* Name of linker.  */
  2096. #ifndef LD_NAME
  2097. #define LD_NAME "ld"
  2098. #endif
  2099.  
  2100. /* Temporary file name base.  */
  2101. static char *temp_filename;
  2102.  
  2103. /* The user has specified several input files.  Invoke the linker to
  2104.    link them all together, and convert and delete the resulting output
  2105.    file.  */
  2106.  
  2107. static char *
  2108. link_inputs (inputs, ld)
  2109.      struct string_list *inputs;
  2110.      char *ld;
  2111. {
  2112.   size_t c;
  2113.   struct string_list *q;
  2114.   char **argv;
  2115.   size_t i;
  2116.   int pid;
  2117.   int status;
  2118.  
  2119.   c = 0;
  2120.   for (q = inputs; q != NULL; q = q->next)
  2121.     ++c;
  2122.  
  2123.   argv = (char **) alloca ((c + 5) * sizeof(char *));
  2124.  
  2125. #ifndef __MSDOS__
  2126.   if (ld == NULL)
  2127.     {
  2128.       char *p;
  2129.  
  2130.       /* Find the linker to invoke based on how nlmconv was run.  */
  2131.       p = program_name + strlen (program_name);
  2132.       while (p != program_name)
  2133.     {
  2134.       if (p[-1] == '/')
  2135.         {
  2136.           ld = (char *) xmalloc (p - program_name + strlen (LD_NAME) + 1);
  2137.           memcpy (ld, program_name, p - program_name);
  2138.           strcpy (ld + (p - program_name), LD_NAME);
  2139.           break;
  2140.         }
  2141.       --p;
  2142.     }
  2143.     }
  2144. #endif
  2145.  
  2146.   if (ld == NULL)
  2147.     ld = (char *) LD_NAME;
  2148.  
  2149.   choose_temp_base ();
  2150.  
  2151.   unlink_on_exit = xmalloc (strlen (temp_filename) + 3);
  2152.   sprintf (unlink_on_exit, "%s.O", temp_filename);
  2153.  
  2154.   argv[0] = ld;
  2155.   argv[1] = (char *) "-Ur";
  2156.   argv[2] = (char *) "-o";
  2157.   argv[3] = unlink_on_exit;
  2158.   i = 4;
  2159.   for (q = inputs; q != NULL; q = q->next, i++)
  2160.     argv[i] = q->string;
  2161.   argv[i] = NULL;
  2162.  
  2163.   if (debug)
  2164.     {
  2165.       for (i = 0; argv[i] != NULL; i++)
  2166.     fprintf (stderr, " %s", argv[i]);
  2167.       fprintf (stderr, "\n");
  2168.     }
  2169.  
  2170.   pid = pexecute (ld, argv);
  2171.  
  2172.   if (waitpid (pid, &status, 0) < 0)
  2173.     {
  2174.       perror ("waitpid");
  2175.       unlink (unlink_on_exit);
  2176.       exit (1);
  2177.     }
  2178.  
  2179.   if (status != 0)
  2180.     {
  2181.       fprintf (stderr, "%s: Execution of %s failed\n", program_name, ld);
  2182.       unlink (unlink_on_exit);
  2183.       exit (1);
  2184.     }
  2185.  
  2186.   return unlink_on_exit;
  2187. }
  2188.  
  2189. /* Choose a temporary file name.  Stolen from gcc.c.  */
  2190.  
  2191. static const char *
  2192. choose_temp_base_try (try, base)
  2193.      const char *try;
  2194.      const char *base;
  2195. {
  2196.   const char *rv;
  2197.  
  2198.   if (base)
  2199.     rv = base;
  2200.   else if (try == NULL)
  2201.     rv = NULL;
  2202.   else if (access (try, R_OK | W_OK) != 0)
  2203.     rv = NULL;
  2204.   else
  2205.     rv = try;
  2206.   return rv;
  2207. }
  2208.  
  2209. static void
  2210. choose_temp_base ()
  2211. {
  2212.   const char *base = NULL;
  2213.   int len;
  2214.  
  2215.   base = choose_temp_base_try (getenv ("TMPDIR"), base);
  2216.   base = choose_temp_base_try (getenv ("TMP"), base);
  2217.   base = choose_temp_base_try (getenv ("TEMP"), base);
  2218.  
  2219. #ifdef P_tmpdir
  2220.   base = choose_temp_base_try (P_tmpdir, base);
  2221. #endif
  2222.  
  2223.   base = choose_temp_base_try ("/usr/tmp", base);
  2224.   base = choose_temp_base_try ("/tmp", base);
  2225.  
  2226.   /* If all else fails, use the current directory! */  
  2227.   if (base == NULL)
  2228.     base = "./";
  2229.  
  2230.   len = strlen (base);
  2231.   temp_filename = xmalloc (len + sizeof("/ccXXXXXX") + 1);
  2232.   strcpy (temp_filename, base);
  2233.   if (len > 0 && temp_filename[len-1] != '/')
  2234.     temp_filename[len++] = '/';
  2235.   strcpy (temp_filename + len, "ccXXXXXX");
  2236.  
  2237.   mktemp (temp_filename);
  2238.   if (*temp_filename == '\0')
  2239.     abort ();
  2240. }
  2241.  
  2242. /* Execute a job.  Stolen from gcc.c.  */
  2243.  
  2244. #ifndef OS2
  2245. #ifdef __MSDOS__
  2246.  
  2247. static int
  2248. pexecute (program, argv)
  2249.      char *program;
  2250.      char *argv[];
  2251. {
  2252.   char *scmd, *rf;
  2253.   FILE *argfile;
  2254.   int i;
  2255.  
  2256.   scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 10);
  2257.   rf = scmd + strlen(program) + 2 + el;
  2258.   sprintf (scmd, "%s.exe @%s.gp", program, temp_filename);
  2259.   argfile = fopen (rf, "w");
  2260.   if (argfile == 0)
  2261.     pfatal_with_name (rf);
  2262.  
  2263.   for (i=1; argv[i]; i++)
  2264.     {
  2265.       char *cp;
  2266.       for (cp = argv[i]; *cp; cp++)
  2267.     {
  2268.       if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
  2269.         fputc ('\\', argfile);
  2270.       fputc (*cp, argfile);
  2271.     }
  2272.       fputc ('\n', argfile);
  2273.     }
  2274.   fclose (argfile);
  2275.  
  2276.   i = system (scmd);
  2277.  
  2278.   remove (rf);
  2279.   
  2280.   if (i == -1)
  2281.     {
  2282.       perror (program);
  2283.       return MIN_FATAL_STATUS << 8;
  2284.     }
  2285.  
  2286.   return i << 8;
  2287. }
  2288.  
  2289. #else /* not __MSDOS__ */
  2290.  
  2291. static int
  2292. pexecute (program, argv)
  2293.      char *program;
  2294.      char *argv[];
  2295. {
  2296.   int pid;
  2297.   int retries, sleep_interval;
  2298.  
  2299.   /* Fork a subprocess; wait and retry if it fails.  */
  2300.   sleep_interval = 1;
  2301.   for (retries = 0; retries < 4; retries++)
  2302.     {
  2303.       pid = vfork ();
  2304.       if (pid >= 0)
  2305.     break;
  2306.       sleep (sleep_interval);
  2307.       sleep_interval *= 2;
  2308.     }
  2309.  
  2310.   switch (pid)
  2311.     {
  2312.     case -1:
  2313. #ifdef vfork
  2314.       perror ("fork");
  2315. #else
  2316.       perror ("vfork");
  2317. #endif
  2318.       exit (1);
  2319.       /* NOTREACHED */
  2320.       return 0;
  2321.  
  2322.     case 0: /* child */
  2323.       /* Exec the program.  */
  2324.       execvp (program, argv);
  2325.       perror (program);
  2326.       exit (1);
  2327.       /* NOTREACHED */
  2328.       return 0;
  2329.  
  2330.     default:
  2331.       /* Return child's process number.  */
  2332.       return pid;
  2333.     }
  2334. }
  2335.  
  2336. #endif /* not __MSDOS__ */
  2337. #else /* not OS2 */
  2338.  
  2339. static int
  2340. pexecute (program, argv)
  2341.      char *program;
  2342.      char *argv[];
  2343. {
  2344.   return spawnvp (1, program, argv);
  2345. }
  2346. #endif /* not OS2 */
  2347.